import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 修改密码对话框
 */
public class ChangePasswordDialog extends JDialog {
    private JTextField usernameField;
    private JPasswordField oldPasswordField;
    private JPasswordField newPasswordField;
    private JPasswordField confirmPasswordField;
    private JButton confirmButton;
    private JButton cancelButton;

    public ChangePasswordDialog(JFrame parent) {
        super(parent, "修改密码", true);
        setupUI();
        setLocationRelativeTo(parent);
    }

    private void setupUI() {
        setSize(400, 300);
        setLayout(new BorderLayout());

        // 主面板
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBackground(Color.WHITE);
        mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        // 用户名
        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setFont(HotelManagementSystem.getButtonFont());
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        mainPanel.add(usernameLabel, gbc);

        usernameField = new JTextField(15);
        usernameField.setFont(HotelManagementSystem.getButtonFont());
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(usernameField, gbc);

        // 原密码
        JLabel oldPasswordLabel = new JLabel("原密码:");
        oldPasswordLabel.setFont(HotelManagementSystem.getButtonFont());
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        mainPanel.add(oldPasswordLabel, gbc);

        oldPasswordField = new JPasswordField(15);
        oldPasswordField.setFont(HotelManagementSystem.getButtonFont());
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(oldPasswordField, gbc);

        // 新密码
        JLabel newPasswordLabel = new JLabel("新密码:");
        newPasswordLabel.setFont(HotelManagementSystem.getButtonFont());
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE;
        mainPanel.add(newPasswordLabel, gbc);

        newPasswordField = new JPasswordField(15);
        newPasswordField.setFont(HotelManagementSystem.getButtonFont());
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(newPasswordField, gbc);

        // 确认密码
        JLabel confirmPasswordLabel = new JLabel("确认密码:");
        confirmPasswordLabel.setFont(HotelManagementSystem.getButtonFont());
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.NONE;
        mainPanel.add(confirmPasswordLabel, gbc);

        confirmPasswordField = new JPasswordField(15);
        confirmPasswordField.setFont(HotelManagementSystem.getButtonFont());
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        mainPanel.add(confirmPasswordField, gbc);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.setBackground(Color.WHITE);

        confirmButton = createStyledButton("确认", HotelManagementSystem.getSuccessColor());
        confirmButton.addActionListener(new ConfirmActionListener());
        buttonPanel.add(confirmButton);

        cancelButton = createStyledButton("取消", HotelManagementSystem.getAccentColor());
        cancelButton.addActionListener(e -> dispose());
        buttonPanel.add(cancelButton);

        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        gbc.insets = new Insets(20, 0, 0, 0);
        mainPanel.add(buttonPanel, gbc);

        add(mainPanel, BorderLayout.CENTER);
    }

    private JButton createStyledButton(String text, Color backgroundColor) {
        JButton button = new JButton(text);
        button.setFont(HotelManagementSystem.getButtonFont());
        button.setBackground(backgroundColor);
        button.setForeground(Color.WHITE);
        button.setBorder(new EmptyBorder(8, 15, 8, 15));
        button.setFocusPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));

        // 添加鼠标悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(backgroundColor.darker());
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(backgroundColor);
            }
        });

        return button;
    }

    private class ConfirmActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String username = usernameField.getText().trim();
            String oldPassword = new String(oldPasswordField.getPassword());
            String newPassword = new String(newPasswordField.getPassword());
            String confirmPassword = new String(confirmPasswordField.getPassword());

            if (username.isEmpty() || oldPassword.isEmpty() || newPassword.isEmpty() || confirmPassword.isEmpty()) {
                JOptionPane.showMessageDialog(ChangePasswordDialog.this, "请填写所有字段！", "提示", JOptionPane.WARNING_MESSAGE);
                return;
            }

            if (!newPassword.equals(confirmPassword)) {
                JOptionPane.showMessageDialog(ChangePasswordDialog.this, "新密码和确认密码不一致！", "错误",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (newPassword.length() < 6) {
                JOptionPane.showMessageDialog(ChangePasswordDialog.this, "新密码长度不能少于6位！", "错误",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (DataManager.getInstance().changeAdminPassword(username, oldPassword, newPassword)) {
                JOptionPane.showMessageDialog(ChangePasswordDialog.this, "密码修改成功！", "成功",
                        JOptionPane.INFORMATION_MESSAGE);
                dispose();
            } else {
                JOptionPane.showMessageDialog(ChangePasswordDialog.this, "用户名或原密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}